package com.bd.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import com.bd.constants.ResourcesConstants;
import com.bd.view.CreationWindow;

public class AppPopupMenu<T> extends JPopupMenu {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 4831225300184003135L;
	private JMenuItem			mnAdd				= new JMenuItem("Adicionar", ResourcesConstants.ICON_ADD);
	private JMenuItem			mnEdit				= new JMenuItem("Editar", ResourcesConstants.ICON_EDIT_16);
	private JMenuItem			mnRemove			= new JMenuItem("Remover", ResourcesConstants.ICON_CLOSE_16);
	private IAppPanel<T>		currentPanel;
	private Class<T>			entityClass;

	public AppPopupMenu(IAppPanel<T> panel, Class<T> clz) {
		this.currentPanel = panel;
		entityClass = clz;
		add(mnAdd);
		add(mnEdit);
		add(mnRemove);
		mnAdd.addActionListener(new MnAddActionListener());
		mnEdit.addActionListener(new MnEditListener());
		mnRemove.addActionListener(new MnRemoverListener());
	}

	class MnAddActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			new CreationWindow(entityClass).setVisible(true);
		}
	}

	class MnEditListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			// new CreationWindow(getEntityClass(),
			// currentPanel.getSelectedObject());
		}
	}

	class MnRemoverListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			currentPanel.removeObject(currentPanel.getSelectedObject());
			currentPanel.populateTable();
		}
	}

}
